<%@ Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<SolutionModel>" %>
<%@ Import Namespace="System.Diagnostics"%>

<asp:Content ContentPlaceHolderID="TitleContent" runat="server">
	<%= (string)GetLocalResourceObject("PageTitle") %>
</asp:Content>
<asp:Content ContentPlaceHolderID="PageHeader" runat="server">
    <%= GetLocalResourceObject("PageTitle").ToString()%>
</asp:Content>
<asp:Content ContentPlaceHolderID="MainContent" runat="server">
    <script language="javascript" type="text/javascript">
        var apiURL = "http://chart.apis.google.com/chart?cht=tx&chs=1x0&chf=bg,s,FFFFFF00&chco=000000&chl=";
        var formula = "\\[ \\left\\{ \\begin{array}{l l} \\Delta u = f\\\\ u = {g} \\quad \\text{ on $x$={x1}, $y$={y1}, $z$={z1}, $x$={x2}, $y$={y2}, $z$={z2}}  \\\\ \\end{array} \\right.\\]";

        $(function() {
            $("#inpX1").keyup(function(e) {
                updateFormula();
            });
            $("#inpX2").keyup(function(e) {
                updateFormula();
            });
            $("#inpY1").keyup(function(e) {
                updateFormula();
            });
            $("#inpY2").keyup(function(e) {
                updateFormula();
            });
            $("#inpZ1").keyup(function(e) {
                updateFormula();
            });
            $("#inpZ2").keyup(function(e) {
                updateFormula();
            });
            $("#inpG").keyup(function(e) {
                updateFormula();
            });
        });

        function updateFormula() {
            var vars = [["#inpX1", "{x1}"],
                        ["#inpX2", "{x2}"],
                        ["#inpY1", "{y1}"], 
                        ["#inpY2", "{y2}"],
                        ["#inpZ1", "{z1}"],
                        ["#inpZ2", "{z2}"],
                        ["#inpG", "{g}"], ];
            
            var f = formula;
            for (var i in vars) {
                var v = vars[i];
                f = insertValueInFormula(f, v[0], v[1]);
            }
            $("#imgFormula").attr("src", apiURL + f);
            $("#imgFormula").attr("alt", f);
        }

        function insertValueInFormula(f, element, pattern) {
            var text = $(element).val();
            return f.replace(pattern, text);
        }
    </script>
    
    <%
        using (Html.BeginForm("Index", "Solution", new {}, FormMethod.Post, new { id = "solveEquation" }))
        { %>
            <div class="problem">
                <fieldset class="fs-input-params">
                    <legend><%= (string)GetLocalResourceObject("InputParams")%></legend>
                    <table class="form-input-params">
                        <tr>
                            <td class="label">
                                <label for="<%= Reflector.GetPropertyName<SolutionModel>(x => x.Problem.InputParameters.N) %>">
                                    <%= (string)GetLocalResourceObject("PointsCount")%>
                                </label>
                            </td>
                            <td class="value">
                                <%= Html.TextBoxFor(x => x.Problem.InputParameters.N ) %>
                            </td>
                            <td class="hint"><%= (string)GetLocalResourceObject("PointsCount.Hint")%></td>
                        </tr>
                        <tr>
                            <td class="label">
                                <label for="inpX1">
                                    <%= (string)GetLocalResourceObject("x1")%>
                                </label>
                            </td>
                            <td class="value">
                                <%= Html.TextBoxFor(x => x.Problem.InputParameters.X1, new { id = "inpX1" })%>
                            </td>
                        </tr>
                        <tr>
                            <td class="label">
                                <label for="inpX2">
                                    <%= (string)GetLocalResourceObject("x2")%>
                                </label>
                            </td>
                            <td class="value">
                                <%= Html.TextBoxFor(x => x.Problem.InputParameters.X2, new { id = "inpX2" })%>
                            </td>
                        </tr>
                        <tr>
                            <td class="label">
                                <label for="inpY1">
                                    <%= (string)GetLocalResourceObject("y1")%>
                                </label>
                            </td>
                            <td class="value">
                                <%= Html.TextBoxFor(x => x.Problem.InputParameters.Y1, new { id = "inpY1" })%>
                            </td>
                        </tr>
                        <tr>
                            <td class="label">
                                <label for="inpY2">
                                    <%= (string)GetLocalResourceObject("y2")%>
                                </label>
                            </td>
                            <td class="value">
                                <%= Html.TextBoxFor(x => x.Problem.InputParameters.Y2, new { id = "inpY2" })%>
                            </td>
                        </tr>
                        <tr>
                            <td class="label">
                                <label for="inpZ1">
                                    <%= (string)GetLocalResourceObject("z1")%>
                                </label>
                            </td>
                            <td class="value">
                                <%= Html.TextBoxFor(x => x.Problem.InputParameters.Z1, new { id = "inpZ1" })%>
                            </td>
                        </tr>
                        <tr>
                            <td class="label">
                                <label for="inpZ2">
                                    <%= (string)GetLocalResourceObject("z2")%>
                                </label>
                            </td>
                            <td class="value">
                                <%= Html.TextBoxFor(x => x.Problem.InputParameters.Z2, new { id = "inpZ2" })%>
                            </td>
                        </tr>
                        <tr>
                            <td class="label">
                                <label for="f">
                                    <%= (string)GetLocalResourceObject("LeftPart")%>
                                </label>
                            </td>
                            <td class="value"><%= Html.TextBox("f", "") %></td>
                            <td class="hint"><%= (string)GetLocalResourceObject("LeftPart.Hint")%></td>
                        </tr>
                        <tr>
                            <td class="label">
                                <label for="inpG">
                                    <%= (string)GetLocalResourceObject("ValueOnBoundary")%>
                                </label>
                            </td>
                            <td class="value"><%= Html.TextBoxFor(x => x.Problem.InputParameters.G, new { id = "inpG" })%></td>
                            <td class="hint"><%= (string)GetLocalResourceObject("ValueOnBoundary.Hint")%></td>
                        </tr>
                        <tr>
                            <td class="label">
                                <label for="<%= Reflector.GetPropertyName<SolutionModel>(x => x.Problem.InputParameters.Epsilon) %>">
                                    <%= (string)GetLocalResourceObject("Epsilon")%>
                                </label>
                            </td>
                            <td class="value"><%= Html.TextBoxFor(x => x.Problem.InputParameters.Epsilon)%></td>
                            <td class="hint"><%= (string)GetLocalResourceObject("Epsilon.Hint")%></td>
                        </tr>
                        <tr>
                            <td class="label">
                                <label for="<%= Reflector.GetPropertyName<SolutionModel>(x => x.Problem.InputParameters.H) %>">
                                    <%= (string)GetLocalResourceObject("H")%>
                                </label>
                            </td>
                            <td class="value"><%= Html.TextBoxFor(x => x.Problem.InputParameters.H)%></td>
                            <td class="hint"><%= (string)GetLocalResourceObject("H.Hint")%></td>
                        </tr>
                        <tr>
                            <td class="label">
                                <label for="<%= Reflector.GetPropertyName<SolutionModel>(x => x.Problem.InputParameters.Alpha) %>">
                                    <%= (string)GetLocalResourceObject("Alpha")%>
                                </label>
                            </td>
                            <td class="value"><%= Html.TextBoxFor(x => x.Problem.InputParameters.Alpha)%></td>
                            <td class="hint"><%= (string)GetLocalResourceObject("Alpha.Hint")%></td>
                        </tr>
                    </table>
                </fieldset>
                <fieldset class="fs-problem">
                    <legend><%= (string)GetLocalResourceObject("Problem")%></legend>
                        <img id="imgFormula" title="<%= (string)GetLocalResourceObject("Problem")%>"
                             src="http://chart.apis.google.com/chart?cht=tx&chs=1x0&chf=bg,s,FFFFFF00&chco=000000&chl=\[ \left\{ \begin{array}{l l} \Delta u = f\\ u= <%= Model.Problem.InputParameters.G %> \quad \text{ on $x$=<%= Model.Problem.InputParameters.X1 %>, $y$=<%= Model.Problem.InputParameters.Y1 %>, $z$=<%= Model.Problem.InputParameters.Z1 %>, $x$=<%= Model.Problem.InputParameters.X2 %>, $y$=<%= Model.Problem.InputParameters.Y2 %>, $z$=<%= Model.Problem.InputParameters.Z2 %>}  \\ \end{array} \right.\]">
                </fieldset>
                <input type="submit" value="<%= (string)GetLocalResourceObject("Button.Solve")%>" />
            </div>
    <%  }%> 
    
    <% if(Model.State == SolutionModel.ModelState.Solved)
       {
           %>
           <div class="solution">
               <%= (string)GetLocalResourceObject("IterationsCount")%> <%= Model.Problem.Solution.IterationsCount %>
               (<%= Model.Problem.Solution.ExecutionTime %>)
               <table class="header">
                    <tr>
                        <th>X</th>
                        <th>Y</th>
                        <th>Z</th>
                        <th class="last">U</th>
                    </tr>
                </table>
                <div class="body">
                    <table>
                  <%
                      foreach (var point in Model.Problem.Solution.U)
                  {
                      %>
                      <tr>
                        <td><div><%= point.X %></div></td>
                        <td><div><%= point.Y %></div></td>
                        <td><div><%= point.Z %></div></td>
                        <td><div><%= point.U %></div></td>
                      </tr>
                      <%
                  }
                       %>
                </table>
                </div>
           </div>
           <%

           var value = new StringBuilder();
           var z0 = Model.Problem.Solution.U.Where(p => p.Z != 0).First().Z;
               var points = Model.Problem.Solution.U.Where(p => p.Z == z0).Select(p => p);

               foreach (var u in points)
               {
                   value.AppendFormat("{0}/{1}/{2}|", u.X.ToString().Replace(',','.'),
                                            u.Y.ToString().Replace(',', '.'),
                                            u.U.ToString().Replace(',', '.'));
               }
           %>
           <script type="text/javascript">
               function onSilverlightError(sender, args) {
                   var appSource = "";
                   if (sender != null && sender != 0) {
                       appSource = sender.getHost().Source;
                   }

                   var errorType = args.ErrorType;
                   var iErrorCode = args.ErrorCode;

                   if (errorType == "ImageError" || errorType == "MediaError") {
                       return;
                   }

                   var errMsg = "Unhandled Error in Silverlight Application " + appSource + "\n";

                   errMsg += "Code: " + iErrorCode + "    \n";
                   errMsg += "Category: " + errorType + "       \n";
                   errMsg += "Message: " + args.ErrorMessage + "     \n";

                   if (errorType == "ParserError") {
                       errMsg += "File: " + args.xamlFile + "     \n";
                       errMsg += "Line: " + args.lineNumber + "     \n";
                       errMsg += "Position: " + args.charPosition + "     \n";
                   }
                   else if (errorType == "RuntimeError") {
                       if (args.lineNumber != 0) {
                           errMsg += "Line: " + args.lineNumber + "     \n";
                           errMsg += "Position: " + args.charPosition + "     \n";
                       }
                       errMsg += "MethodName: " + args.methodName + "     \n";
                   }

                   throw new Error(errMsg);
               }
    </script>
    <div id="silverlightControlHost" style="height:690px; width:635px; margin:0px auto;">
        <object data="data:application/x-silverlight-2," type="application/x-silverlight-2" width="100%" height="100%">
		  <param name="source" value="<%=ResolveClientUrl("~/ClientBin/BSU.GraduateWork.Visualizer.xap")%>"/>
		  <param name="onError" value="onSilverlightError" />
		  <param name="background" value="white" />
		  <param name="initParams" value="dataSource=<%= value %>" />
		  <param name="minRuntimeVersion" value="3.0.40624.0" />
		  <param name="autoUpgrade" value="true" />
		  <a href="http://go.microsoft.com/fwlink/?LinkID=149156&v=3.0.40624.0" style="text-decoration:none">
 			  <img src="http://go.microsoft.com/fwlink/?LinkId=108181" alt="Get Microsoft Silverlight" style="border-style:none"/>
		  </a>
	    </object><iframe id="_sl_historyFrame" style="visibility:hidden;height:0px;width:0px;border:0px"></iframe></div>
           <%
  } %>
</asp:Content>
